﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Microsoft.Surface;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;

namespace ENMAX_ArcGIS
{
    /// <summary>
    /// Interaction logic for ArcGisMapTip.xaml
    /// </summary>
    public partial class ArcGisMapTip : ScatterViewItem
    {
        TwoPointScatterView parentLayer = null;
        TroubleTicketPin ttp;


        public ArcGisMapTip(TwoPointScatterView parent, TroubleTicketPin pin)
        {
            InitializeComponent();

            parentLayer = parent;

            ttp = pin;
            TT_Num.Text = pin.Number;
            TT_SEV.Text = pin.getTroubleTicketSeverityString();

            /* Change the background color of the severity text accordingly */
            switch (TT_SEV.Text)
            {
                case "High":
                    TT_SEV.Foreground = new SolidColorBrush(Colors.Red);
                    SS_SEV.Value = 1;
                    break;
                case "Medium":
                    TT_SEV.Foreground = new SolidColorBrush(Colors.Yellow);
                    SS_SEV.Value = 2;
                    break;
                case "Low":
                    TT_SEV.Foreground = new SolidColorBrush(Colors.Green);
                    SS_SEV.Value = 3;
                    break;
                default:
                    TT_SEV.Foreground = new SolidColorBrush(Colors.Gray);
                    break;
            }

            TT_DES.Text = pin.Description;
            TT_LOC.Text = pin.TroubleTicket.MapCoordinate.ToString();

            ShowMapButton.Visibility = Visibility.Visible;
        }

        //public ArcGisMapTip(TroubleTicket tt)
        //{
        //    InitializeComponent();

        //    //ttp = pin;
        //    TT_Num.Text = tt.Number;
        //    TT_SEV.Text = tt.getTroubleTicketSeverityString();
        //    TT_DES.Text = tt.Description;
        //    TT_LOC.Text = tt.MapCoordinate.ToString();
            
        //}

        private void EditButton_Click(object sender, RoutedEventArgs e)
        {
            if (EditButton.Content.Equals("Edit"))
            {
                TT_DES.IsEnabled = true;
                SS_SEV.Visibility = Visibility.Visible;
                EditButton.Background = new SolidColorBrush(Colors.Red);
                EditButton.Content = "Done Edit";
                /* Cannot exit when editing */
                CloseButton.IsEnabled = false;
                CloseButton.Background = new SolidColorBrush(Colors.Gray);
                UpdateLayout();
                return;
            }
            if (EditButton.Content.Equals("Done Edit"))
            {
                TT_DES.IsEnabled = false;
                SS_SEV.Visibility = Visibility.Collapsed;
                EditButton.Background = new SolidColorBrush(Colors.Aqua);
                EditButton.Content = "Edit";
                /* Re-enable the close button to allow the user to close the MapTip */
                CloseButton.IsEnabled = true;
                CloseButton.Background = new SolidColorBrush(Colors.White);
                /* Update the trouble ticket */
                //ttp.TroubleTicket.Description = TT_DES.Text;
                UpdateLayout();
                return;
            }
        }

        private void ShowMapButton_Click(object sender, RoutedEventArgs e)
        {
            if (parentLayer == null) return;

            // FIX ME: the orientation and position of the map frame
            parentLayer.CreateMapFrameFromTroublePin(ttp.TroubleTicket, new Point(768-ttp.X,ttp.Y), Orientation);
            Visibility = Visibility.Hidden;
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Hidden;
        }

        private void SS_SEV_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (e.OldValue == 0.0) return;

            int newValue = (int)e.NewValue;

            switch (newValue)
            {
                case 1:
                    TT_SEV.Foreground = new SolidColorBrush(Colors.Red);
                    TT_SEV.Text = "High";
                    ttp.Severity = TroubleTicket.TroubleTicketSeverity.High;
                    ttp.Background = new SolidColorBrush(Colors.Red);
                    break;
                case 2:
                    TT_SEV.Foreground = new SolidColorBrush(Colors.Yellow);
                    TT_SEV.Text = "Medium";
                    ttp.Severity = TroubleTicket.TroubleTicketSeverity.Medium;
                    ttp.Background = new SolidColorBrush(Colors.Yellow);
                    break;
                case 3:
                    TT_SEV.Foreground = new SolidColorBrush(Colors.Green);
                    TT_SEV.Text = "Low";
                    ttp.Severity = TroubleTicket.TroubleTicketSeverity.Low;
                    ttp.Background = new SolidColorBrush(Colors.Green);
                    break;
                default:
                    TT_SEV.Foreground = new SolidColorBrush(Colors.Gray);
                    ttp.Background = new SolidColorBrush(Colors.Gray);
                    TT_SEV.Text = "N/A";
                    break;
            }

        }
    }
}